June 11, 2021
These blogs will help you learn Java Programming & Concepts in a simple and effective way. If you have no prior knowledge in Java, you won’t face any difficulty. If you are experienced java developer, this blog will help you brush up the concepts.
public class Test extends Thread {
public void run(){
// logic
}
Thread t1 = new Test();
// Thread t1 = new Thread(new Test()); FOR RUNNABLE
t1.Start();
}
Object.wait() → Thread will be blocked till notify is called.
Object.notify() → Wakes up a single thread that is waiting on this object’s monitor and put it in runnable mode.
Object.notifyAll() → Wakes up all threads that is waiting on this object’s monitor.
Thread.yield() → Puts current thread to runnable and takes next thread.
Thread.sleep() → Puts the thread on sleep/suspended state for few milliseconds, we can pass time to the sleep method as parameter.
Tool used for thread synchronization. A semaphore is a very relaxed type of lockable object. A given semaphore has a predefined maximum count, and a current count. Lock provides mutual exclusion, but does not serve queues or ordering problem like printing jobs or producer consumer problems. For this we have Semaphore.
public class Semaphore {
int value;
// No of users that can use Resource
public Semaphore(int init) {
if (init < 0) {
init = 0;
}
value = init;
}
// Acquiring Resource
public synchronized void down() {
while (value == 0) {
try {
wait();
} catch (InterruptedException e) {
}
value — ;
}
}
// Releasing Resource
public synchronized void up() {
value++;
notify();
}
}
We will initialize Semaphore class before Start() method, in the run we will first call semaphoreObject.down() to lock and task completion we call semaphoreObject.up().
A mutex is a “Mutual Exclusion Semaphore”. It refers to a type of lockable object that can be owned by exactly one thread at a time. Only the thread that acquired the lock can release the lock on a mutex. When the mutex is locked, any attempt to acquire the lock will fail or block, even if that attempt is done by the same thread.
public class MyException extends Exception {
private String errorCode = “Unkown_Exception”;
public MyException(String message, String errorCode){
super(message);
this.errorCode = errorCode;
}
Public String getErrorCode(){
Return this.errorCode;
}
}
Have an Observable class.
There are 3 JDBC statements:
@Resource(mappedName = “jdbc/DarmAircom”)
private javax.sql.DataSource dataSource;
Connection connection = dataSource.getConnection();
CallableStatement statement = connection.prepareCall(“call ABC.ABC_PKG.PROCEDURE_NAME(?,?,?,?,?,?,?,?,?,?,?,?,?)”);
statement.execute();
statement.close();
connection.close();
ByteStream: For reading and writing binary data, byte stream is incorporated.
CharacterStreams: It work with the characters rather than the byte.
FileInputStream: It contains the input byte from a file and implements an input stream.
FileOutputStream: It uses for writing data to a file and also implements an output stream.
Reading from File: A FileReader class is a general tool to read in characters from a File. The BufferedReader class can wrap around Readers, like FileReader, to buffer the input and improve efficiency.
public static void readFromFile(String fileName) throws IOException {
int total = 0;
BufferedReader in = new BufferedReader( new FileReader(fileName));
for ( String s = in.readLine(); s != null; s = in.readLine() ) {
// GOT EACH LINE
}
in.close();
}
File name would be something like “C:/Users/Atharva/Desktop/task.txt”
Writing to File: Check out the example for writing to file using FileOutputStream.
File fout = new File(file_location_string);
FileOutputStream fos = new FileOutputStream(fout);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fos));
out.write(“something”);
Using FileWriter:
FileWriter fstream = new FileWriter(file_location_string);
BufferedWriter out = new BufferedWriter(fstream);
out.write(“something”);
FileOutputStream is meant for writing streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter.
public class Singleton {
private static Singleton instance = new Singleton();
private singleton() {}
public static Singleton getInstance(){
return instance;
}
}
Factory pattern is one of the most used design patterns in Java. In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface. For example:
interface Dog {
public void speak ();
}
class Poodle implements Dog {
public void speak() {
System.out.println(“The poodle says \”arf\””);
}
}
class Rottweiler implements Dog {
public void speak() {
System.out.println(“The Rottweiler says (in a very deep voice) \”WOOF!\””);
}
}
class DogFactory {
public static Dog getDog(String criteria) {
if ( criteria.equals(“small”) )
return new Poodle();
else if ( criteria.equals(“big”) )
return new Rottweiler();
return null;
}
}
public class JavaFactoryPatternExample {
public static void main(String[] args) {
Dog dog = DogFactory.getDog(“small”);
dog.speak();
dog = DogFactory.getDog(“big”);
dog.speak();
}
}
It’s used so that two unrelated interfaces can work together. Example:
public interface SocketAdapter {
public Volt get120Volt();
public Volt get12Volt();
public Volt get3Volt();
}
Builder pattern is the extension of Factory pattern wherein the Builder class builds a complex object in multiple steps.